package com.hly.musicbackground.model.auth.rest;

import com.hly.musicbackground.model.auth.params.LoginParams;
import com.hly.musicbackground.model.auth.params.RegisterParams;
import com.hly.musicbackground.model.auth.service.LoginService;
import com.hly.musicbackground.model.auth.service.VerificationMailService;
import com.hly.musicbackground.model.user.params.MailParams;
import com.hly.musicbackground.model.user.params.VerifyMailParams;
import com.hly.musicbackground.result.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;

/**
 * 认证用户
 * */
@RestController
@RequestMapping("/api/auth")
@Api(tags = "认证用户")
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
public class AuthController {

    private final LoginService loginService;
    private final VerificationMailService verificationMailService;

    /**
     * 登录
     * */
    @PostMapping("/login")
    @ApiOperation(value = "登录")
    public Result login(@RequestBody LoginParams params,
                                   HttpServletResponse response){
        response.setHeader("Access-Control-Expose-Headers" , "token");
        response.setHeader("token", loginService.login(params));
        return Result.ok();
    }

    /**
     * 注册
     * */
    @PostMapping("/register")
    @ApiOperation("注册")
    public Result register(@RequestBody RegisterParams params,
                                    HttpServletResponse response){
        response.setHeader("Access-Control-Expose-Headers" , "token");
        response.setHeader("token", loginService.register(params));
        return Result.ok();
    }

    /**
     * 发送邮箱验证码
     * */
    @GetMapping("/verificationMail/{type}")
    @ApiOperation("发送邮箱验证码")
    public Result verificationMail(MailParams params,
                                              @PathVariable("type") String type){
        verificationMailService.sendMail(params.getMail(), type);
        return Result.ok();
    }

    /**
     * 效验验证码
     * */
    @GetMapping("/verifyMail")
    @ApiOperation("效验验证码")
    public Result verifyMail(VerifyMailParams params){
        verificationMailService.isVerificationMail(params.getMail(), params.getSms());
        return Result.ok();
    }
}
